package com.loie.xcx.util;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.loie.xcx.config.TaxCloudConfig;
import com.loie.xcx.mapper.SqlMapper;
import com.loie.xcx.mapper.SysAttachmentMapper;
import com.loie.xcx.model.bo.SysAttachment;
import com.loie.xcx.model.dto.AttachmentDTO;
import com.loie.xcx.model.dto.AttachmentListDTO;
import com.loie.xcx.model.dto.IwinstanceQueryDTO;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.*;
@Slf4j

@Component
public
class AttachmentSave {
    @Autowired
    SqlMapper sqlMapper;
    @Autowired
    private SysAttachmentMapper sysAttachmentMapper;
    @Autowired
    TaxCloudConfig taxCloudConfig;

    public
    AttachmentDTO attachmenttobase64(IwinstanceQueryDTO iwinstanceQueryDTO){
        QueryWrapper<SysAttachment> wrapper = new QueryWrapper<>();
        //List<AttachmentDTO> attachmentListDTO=new ArrayList<>();

        wrapper.eq("module", iwinstanceQueryDTO.getModule())
                .eq("recordid", iwinstanceQueryDTO.getRecordId());

        //List<SysAttachment> sysAttachmentList=sysAttachmentMapper.selectList(wrapper);
        SysAttachment sysAttachment=sysAttachmentMapper.selectOne(wrapper);
       // for (SysAttachment sysAttachment:sysAttachmentList) {
            //sysAttachment = sysAttachmentMapper.selectOne(wrapper);
            String achmentpath = taxCloudConfig.getAttachmentpath();
            log.info(achmentpath);
            String str="";

            File dest1 = new File(achmentpath + sysAttachment.getStoredPath().replace("\\", "/")
                    + sysAttachment.getRecordID() + ".iAttachment");
            if (dest1.exists()) {
                //System.out.println(dest1.getPath());
                str= Base64Util.encryptToBase64(dest1.getPath());


            }
            else str="";
            AttachmentDTO attachmentDTO=new AttachmentDTO();

            attachmentDTO.setRecordid(iwinstanceQueryDTO.getRecordId());

            attachmentDTO.setBase64(str);
            attachmentDTO.setName(sysAttachment.getName());
           // attachmentListDTO.add(attachmentDTO);

        //}
        //System.out.println(attachmentListDTO);

        return attachmentDTO;
    }

}
